package mapReduce.homework.q5_topMoneyEveryYear;

import mapReduce.util.JobSubmit;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.*;

public class TopMoneyEveryYearApp {

    public static class tmeyMapper extends Mapper<Text,NullWritable,Text,Text>{
        @Override
        protected void map(Text key, NullWritable value, Context context) throws IOException, InterruptedException {
            //从序列化文件中读取数据，所以是key.toString()
            String line = key.toString();
            String[] fields = line.split("\t");
            String year = fields[0];
            String orderId = fields[1];
            String partner = fields[2];
            double money = Double.parseDouble(fields[4]);
            //把年份作为key(需要排序)，订单编号，合作方和钱作为value。
            context.write(new Text(year),new Text(orderId+"\t"+partner+"\t"+money));
        }
    }

    public static class tmeyReducer extends Reducer<Text,Text,Text,Text>{

        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            //key:2000
            //value:orderId+"\t"+partner+"\t"+money
            List<String> moneyList = new ArrayList<String>();
            String year = key.toString();


            //同一个年份为一组，同一组下把所有的信息进行处理，把钱放在一个list中
            for(Text value:values){
//                String[] fields = value.toString().split("\t");
//                double money = Double.parseDouble(fields[2]);
                moneyList.add(value.toString());
            }


            Collections.sort(moneyList, new Comparator<String>() {
                public int compare(String o1, String o2) {
                    double thisMoney = Double.parseDouble(o1.split("\t")[2]);
                    double thatMoney = Double.parseDouble(o2.split("\t")[2]);
                    return thisMoney>thatMoney?-1:1;
                }
            });

            //把当前年份(key)，对应的最大钱数输出
            context.write(key,new Text(moneyList.get(0)));
        }

    }

    public static void main(String[] args) throws Exception {
//        Job job = Job.getInstance();
//
//        job.setMapOutputKeyClass(Text.class);
//        job.setMapOutputValueClass(Text.class);
//        job.setOutputKeyClass(Text.class);
//        job.setOutputValueClass(Text.class);
//
//        job.setJarByClass(TopMoneyEveryYearApp.class);
//        job.setMapperClass(tmeyMapper.class);
//        job.setReducerClass(tmeyReducer.class);
//
//        //由于随机抽样需要使用序列化类型文件作为输入，所有通过job设置输入输出的文件类型为SequenceFile
//        job.setInputFormatClass(SequenceFileInputFormat.class);
//
//        Path outpath = new Path(args[1]);
//        FileSystem fs = FileSystem.get(new URI(outpath.toString()),new Configuration());
//        if(fs.exists(outpath)){
//            fs.delete(outpath,true);
//        }
//        FileInputFormat.setInputPaths(job,new Path(args[0]));
//        FileOutputFormat.setOutputPath(job,new Path(args[1]));
//
//        job.setNumReduceTasks(3);
//        job.setPartitionerClass(TotalOrderPartitioner.class);
//        InputSampler.Sampler sampler = new InputSampler.RandomSampler(0.5,1000,3);
//        InputSampler.writePartitionFile(job,sampler);
//
//        job.waitForCompletion(true);

        Object[] objs = {0.5,1000,1};

        JobSubmit.submitJobByCriticalPoint(
                TopMoneyEveryYearApp.class,
                args,
                true,
                objs,
                3
                );
    }

}
